using System.Security.Cryptography.X509Certificates;
using Newtonsoft.Json.Linq;
using Unity.Mathematics;
namespace Cesium3DTilesSelection
{
    public class SkirtMeshMetadata
    {
        public int noSkirtIndicesBegin;
        public int noSkirtIndicesCount;
        public double3 meshCenter;
        public double skirtWestHeight;
        public double skirtSouthHeight;
        public double skirtEastHeight;
        public double skirtNorthHeight;
        public static SkirtMeshMetadata parseFromGltfExtras(JToken extras)
        {
            var skirtIt = extras["skirtMeshMetadata"];
            if (skirtIt == null)
            {
                return null;
            }

            SkirtMeshMetadata skirtMeshMetadata = new SkirtMeshMetadata();

            var gltfSkirtMeshMetadata = skirtIt;
            var pNoSkirtRange = skirtIt["noSkirtRange"] as JArray;
            if (pNoSkirtRange == null || pNoSkirtRange.Count != 2)
            {
                return null;
            }

            int noSkirtIndicesBegin = ((int)pNoSkirtRange[0]);// .getSafeNumberOrDefault<double>(-1.0);
            int noSkirtIndicesCount = ((int)pNoSkirtRange[1]);//.getSafeNumberOrDefault<double>(-1.0);

            skirtMeshMetadata.noSkirtIndicesBegin = noSkirtIndicesBegin;
            skirtMeshMetadata.noSkirtIndicesCount = noSkirtIndicesCount;

            var pMeshCenter = gltfSkirtMeshMetadata["meshCenter"] as JArray;
            if (pMeshCenter == null || pMeshCenter.Count != 3)
            {
                return null;
            }
            var meshCenter = pMeshCenter.ToObject<double[]>();
            if (meshCenter == null) return null;

            skirtMeshMetadata.meshCenter = new double3(meshCenter[0], meshCenter[1], meshCenter[2]);
            double westHeight, southHeight, eastHeight, northHeight;
            try
            {
                westHeight = gltfSkirtMeshMetadata.Value<double>("skirtWestHeight");
                southHeight = gltfSkirtMeshMetadata.Value<double>("skirtSouthHeight");
                eastHeight = gltfSkirtMeshMetadata.Value<double>("skirtEastHeight");
                northHeight = gltfSkirtMeshMetadata.Value<double>("skirtNorthHeight");
            }
            catch (System.Exception e)
            {
                UnityEngine.Debug.LogException(e);
                return null;
            }

            skirtMeshMetadata.skirtWestHeight = westHeight;
            skirtMeshMetadata.skirtSouthHeight = southHeight;
            skirtMeshMetadata.skirtEastHeight = eastHeight;
            skirtMeshMetadata.skirtNorthHeight = northHeight;

            return skirtMeshMetadata;
        }
        public static JObject createGltfExtras(SkirtMeshMetadata skirtMeshMetadata)
        {
            var gltfExtras = new JObject();
            var metadata = new JObject();
            var noSkirtRange = new int[2] { skirtMeshMetadata.noSkirtIndicesBegin, skirtMeshMetadata.noSkirtIndicesCount };
            var meshCenter = new double[3] { skirtMeshMetadata.meshCenter.x, skirtMeshMetadata.meshCenter.y, skirtMeshMetadata.meshCenter.z };

            metadata.Add("noSkirtRange", JArray.FromObject(noSkirtRange));
            metadata.Add("meshCenter", JArray.FromObject(meshCenter));
            metadata.Add("skirtWestHeight", skirtMeshMetadata.skirtWestHeight);
            metadata.Add("skirtSouthHeight", skirtMeshMetadata.skirtSouthHeight);
            metadata.Add("skirtEastHeight", skirtMeshMetadata.skirtEastHeight);
            metadata.Add("skirtNorthHeight", skirtMeshMetadata.skirtNorthHeight);


            gltfExtras.Add("skirtMeshMetadata", metadata);
            return gltfExtras;

        }
    }
}